package com.service;

import com.entity.*;
import com.repository.PatientRepository;
import com.repository.impl.PatientRepositoryImpl;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.Vector;

public class PatientService {
    public PatientService(){}

    PatientRepository patientRepository=new PatientRepositoryImpl();

//    InputStream inputStream = PatientService.class.getClassLoader().getResourceAsStream("config.xml");
//    SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//    SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
//    SqlSession sqlSession = sqlSessionFactory.openSession();
//    PatientRepository patientRepository = sqlSession.getMapper(PatientRepository.class);

    //查找这个病人的信息
    public Patient findThisPatient(String account){
        return patientRepository.patientInfoFindByAccount(account);
    }

    //查找这个病人的挂号记录
    public Vector<RegisterDoc> thisPatientRegisterDocs(String account){
        return patientRepository.findAllThisRegisterDocs(account);
    }


    //判断信息是否填写完整
    public boolean isComplete(String account){
        Patient patient=patientRepository.patientInfoFindByAccount(account);
        if(patient.getPatientPwd()==null||patient.getName()==null||patient.getBornDate()==null||patient.getIdentityId()==null||patient.getPhone()==null||patient.getMICN()==null){
            return false;
        }
        return true;
    }

    //更新补全个人信息
    public int updateThisPatientInfo(Patient patient){
        return patientRepository.updatePatient(patient);
    }



    //判断是否登录成功
    public int patientValidateLoginInfo(Patient patient)
    {
        return patientRepository.isPatientLogin(patient);
    }


    public int patientEnrollLogin(Patient patient){
//        System.out.println(patient.getAccount()+" "+patient.getPatientPwd());
        if(patientRepository.isExistPatientByAccount(patient.getAccount())>0)
            return 2;
        else
            return patientRepository.addPatientLogin(patient);
    }

    //添加挂号
    public int addRegister(RegisterDoc registerDoc) {
        return patientRepository.addRegisterDoc(registerDoc);
    }

    //查找开药记录
    public Vector<Drug> thisPatientDrugs(String account){
        return patientRepository.findThisDrugs(account);
    }

    public Vector<Diagnose> thisPatientDiagnoses(String account) {
        return patientRepository.findThisDiagnoses(account);
    }

    public Vector<Cost> thisPatientCosts(String account) {
        return patientRepository.findThisCosts(account);
    }

    public RegisterDoc findRegisterDocById(String registerId) {
        return patientRepository.findRegisterDoc(registerId);
    }
}
